﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace quanlysinhvien
{
    public partial class FrmUserManagement : Form
    {
        SqlConnection conn;
        public static string ID1;

        public FrmUserManagement()
        {
            InitializeComponent();
        }
        private void LoadDGuser()
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select User_ID,Username,Password from TblUser";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "user");
            
            //dgLoadUser.CurrentRow.Cells[1].ToString();
            //dgLoadUser.CurrentRow.Cells[2].ToString();
            dgLoadUser.DataSource = ds.Tables[0];
            conn.Close();

        }

        private void FrmUserManagement_Load(object sender, EventArgs e)
        {
            LoadDGuser();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmAddUser FrmAddUser = new FrmAddUser();
            FrmAddUser.Show();
            this.Hide();
        }


        private void btnEdit_Click(object sender, EventArgs e)
        {
            
            string User = dgLoadUser.CurrentRow.Cells[1].FormattedValue.ToString();
            string Pass = dgLoadUser.CurrentRow.Cells[2].FormattedValue.ToString();
            conn = Connect.getConnection();
            conn.Open();
            string sql= "update TblUser set Username='" + User + "',Password='" + Pass + "' where User_ID='"+ID1+"'" ;
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            MessageBox.Show("Update successful!");
            conn.Close();
            LoadDGuser();
        }

        private void dgLoadUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            ID1 = dgLoadUser.CurrentRow.Cells[0].FormattedValue.ToString();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "You realy want del this user?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                conn = Connect.getConnection();
                conn.Open();
                string sql = "delete from TblUser where User_ID='" + ID1 + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Deleted!");
                conn.Close();
                LoadDGuser();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            FormMain FormMain = new FormMain();
            FormMain.Show();
            this.Hide();
        }



        
    }
}
